package org.soarsky.wealthchengdu.google.map;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.google.map.BaiduLocation.IGetLocation;
import org.soarsky.wealthchengdu.google.map.MapOption.MapProvider;
import org.soarsky.wealthchengdu.module.guide.Constants;
import org.soarsky.wealthchengdu.view.LoadingDialog;

import android.app.ActivityGroup;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.baidu.location.BDLocation;


public class MapActivityContor extends ActivityGroup implements OnClickListener {
	private static MapActivityContor sMe = null;
	private final String TAG = "MapActivity";
	private SharedPreferences mPrefs;
	private FrameLayout mContainer;
	
	private Button mapStayBtn;// 住宿
	private Button mapDietBtn;// 饮食
	private Button mapTrafficBtn;// 公交
	private ImageButton backBut;
	//private ImageButton moreBut;

	private void initViews() {
		mContainer = (FrameLayout) findViewById(R.id.fl_map_container);
		mapStayBtn = (Button) findViewById(R.id.mapstay);
		mapDietBtn = (Button) findViewById(R.id.mapdiet);
		mapTrafficBtn = (Button) findViewById(R.id.maptraffic);
		backBut = (ImageButton) findViewById(R.id.mapbackbtn);
	//	moreBut = (ImageButton) findViewById(R.id.mapmorebtn);
		mapStayBtn.setTag(Constants.STAYBTN);
		mapDietBtn.setTag(Constants.DIETBTN);
		mapTrafficBtn.setTag(Constants.TRAFFICBTN);
		backBut.setTag(Constants.BRACK);
		//moreBut.setTag(Constants.MORE);
		mapStayBtn.setOnClickListener(this);
		mapDietBtn.setOnClickListener(this);
		mapTrafficBtn.setOnClickListener(this);
		backBut.setOnClickListener(this);
		//moreBut.setOnClickListener(this);

	}

	private void switchMapProvider(MapProvider provider) {
		if (provider == null)
			return;
		Class activityClass = null;
		switch (provider) {
		case Google:
			activityClass = GoogleMapActivity.class;
			break;
		// case Baidu:
		// activityClass = BaiduMapActivity.class;
		// / 、、break;
		default:
			return;
		}
//		if (mMapOption != null) {
//			if (mMapOption.getMapProvider() == provider)
//				return;
			this.getLocalActivityManager().destroyActivity(
					this.getLocalActivityManager().getCurrentId(), true);
	//	}
		mContainer.removeAllViews();
		Intent intent = new Intent(this, activityClass);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View view = this.getLocalActivityManager()
				.startActivity(provider.toString(), intent).getDecorView();
		mContainer.addView(view);
		switch (provider) {
		case Google:
			if (GoogleMapActivity.getInstance() != null) {
//				mMapController = GoogleMapActivity.getInstance()
//						.getMapControler();
			}
			break;
		}
	}


	private void log(String msg) {
		Log.d(TAG, msg);
	}

	private void logE(String msg) {
		Log.e(TAG, msg);
	}

	public MapActivityContor() {
		sMe = this;
	}

	public static MapActivityContor getInstance() {
		return sMe;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		log("onCreate");
		setContentView(R.layout.map_activity);
		mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		initViews();
		MapOption option = new MapOption();
		option.setMapProvider(MapProvider.Google);
		option.setSatellite(mPrefs.getBoolean("map_satellite_key", false));
		option.setStreetView(mPrefs.getBoolean("map_streetview_key", false));
		option.setTraffic(mPrefs.getBoolean("map_traffic_key", false));
		setMapOption(option);
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		log("onDestroy");
		sMe = null;
		
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	public void setMapOption(MapOption option) {
		if (option == null)
			return;
		log("setMapOption->provider:" + option.getMapProvider().toString());
		switchMapProvider(option.getMapProvider());
		//mMapOption = option;
		
	}

	

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (GoogleMapActivity.click)
			return;
		int key = Integer.parseInt(v.getTag().toString());

		// if (LocationMeB()) {
		switch (key) {
		case Constants.BRACK:
			finish();
			break;
		case Constants.STAYBTN:
			GoogleMapActivity.status = Constants.STAYBTN;
			mapStayBtn.setTextColor(Color.RED);
			mapDietBtn.setTextColor(Color.BLACK);
			mapTrafficBtn.setTextColor(Color.BLACK);
			GoogleMapActivity.click = true;
			GoogleMapActivity.getInstance().openMap(DataUtils.LODGING);
			break;
		case Constants.DIETBTN:
			mapStayBtn.setTextColor(Color.BLACK);
			mapDietBtn.setTextColor(Color.RED);
			mapTrafficBtn.setTextColor(Color.BLACK);
			GoogleMapActivity.click = true;
			GoogleMapActivity.getInstance().openMap(DataUtils.FOOD);
			break;
		case Constants.TRAFFICBTN:
			mapStayBtn.setTextColor(Color.BLACK);
			mapDietBtn.setTextColor(Color.BLACK);
			mapTrafficBtn.setTextColor(Color.RED);
			
			
			GoogleMapActivity.click = true;
			GoogleMapActivity.getInstance().openMap(DataUtils.TRAFFIC);
			break;
		
		
		}
		
	}

	
	

}
